import Mock from 'mockjs'

// 生成用户列表数据
const generateUsers = () => {
  const users = []
  for (let i = 1; i <= 50; i++) {
    users.push({
      id: i,
      username: Mock.Random.cname(),
      email: Mock.Random.email(),
      roleId: Mock.Random.pick([1, 2, 3]),
      roleName: Mock.Random.pick(['超级管理员', '普通用户', '访客']),
      status: Mock.Random.pick(['1', '0']),
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    })
  }
  return users
}

let userList = generateUsers()

export default function() {
  // 获取用户列表
  Mock.mock(/\/api\/user\/list/, 'get', (options) => {
    const url = new URL(options.url, 'http://localhost')
    const page = parseInt(url.searchParams.get('page')) || 1
    const pageSize = parseInt(url.searchParams.get('pageSize')) || 10
    const username = url.searchParams.get('username') || ''
    const status = url.searchParams.get('status') || ''
    
    let filteredUsers = [...userList]
    
    // 按用户名筛选
    if (username) {
      filteredUsers = filteredUsers.filter(user => 
        user.username.includes(username)
      )
    }
    
    // 按状态筛选
    if (status) {
      filteredUsers = filteredUsers.filter(user => 
        user.status === status
      )
    }
    
    const total = filteredUsers.length
    const start = (page - 1) * pageSize
    const end = start + pageSize
    const list = filteredUsers.slice(start, end)
    
    return {
      code: 200,
      message: '获取成功',
      data: {
        list,
        total,
        page,
        pageSize
      }
    }
  })

  // 创建用户
  Mock.mock('/api/user/create', 'post', (options) => {
    const userData = JSON.parse(options.body)
    const newUser = {
      id: userList.length + 1,
      ...userData,
      createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    }
    userList.push(newUser)
    
    return {
      code: 200,
      message: '创建成功',
      data: newUser
    }
  })

  // 更新用户
  Mock.mock(/\/api\/user\/update\/\d+/, 'put', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    const updateData = JSON.parse(options.body)
    
    const index = userList.findIndex(user => user.id === id)
    if (index !== -1) {
      userList[index] = { ...userList[index], ...updateData }
      return {
        code: 200,
        message: '更新成功',
        data: userList[index]
      }
    } else {
      return {
        code: 404,
        message: '用户不存在',
        data: null
      }
    }
  })

  // 删除用户
  Mock.mock(/\/api\/user\/delete\/\d+/, 'delete', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    
    const index = userList.findIndex(user => user.id === id)
    if (index !== -1) {
      userList.splice(index, 1)
      return {
        code: 200,
        message: '删除成功',
        data: null
      }
    } else {
      return {
        code: 404,
        message: '用户不存在',
        data: null
      }
    }
  })

  // 获取用户详情
  Mock.mock(/\/api\/user\/detail\/\d+/, 'get', (options) => {
    const url = options.url
    const id = parseInt(url.match(/\d+$/)[0])
    
    const user = userList.find(user => user.id === id)
    if (user) {
      return {
        code: 200,
        message: '获取成功',
        data: user
      }
    } else {
      return {
        code: 404,
        message: '用户不存在',
        data: null
      }
    }
  })
} 